{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Build an Index on Vectors](https://milvus.io/docs/v2.3.0-beta/build_index.md)\n",
    "\n",
    "> This guide describes how to build an index on vectors in Milvus.\n",
    "\n",
    "Vector indexes are an organizational unit of metadata used to accelerate vector similarity search. Without the index built on vectors, Milvus will perform a brute-force search by default.\n",
    "\n",
    "See Vector Index for more information about the mechanism and varieties of vector indexes."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Connect to Milvus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Milvus.Client, 2.2.2-preview.2</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>{MilvusClient:localhost:19530}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Address</td><td><div class=\"dni-plaintext\"><pre>localhost:19530</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: Milvus.Client, 2.2.2-preview.2\"\n",
    "#!import config/Settings.cs\n",
    "\n",
    "using Milvus.Client;\n",
    "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n",
    "\n",
    "//Connect to milvus\n",
    "(string endpoint, int port, string userName, string password) = Settings.LoadFromFile();\n",
    "\n",
    "MilvusClient milvusClient = default;\n",
    "\n",
    "milvusClient = new MilvusClient(endpoint, port, userName, password, null);\n",
    "milvusClient"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> The following example builds a 1024-cluster IVF_FLAT index with Euclidean distance (L2) as the similarity metric. You can choose the index and metrics that suit your scenario. See Similarity Metrics for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "string collectionName = \"book\";\n",
    "MilvusCollection collection = milvusClient.GetCollection(collectionName);\n",
    "await collection.CreateIndexAsync(\n",
    "    \"book_intro\",\n",
    "    //MilvusIndexType.IVF_FLAT,//Use MilvusIndexType.IVF_FLAT.\n",
    "    IndexType.AutoIndex,//Use MilvusIndexType.AUTOINDEX when you are using zilliz cloud.\n",
    "    SimilarityMetricType.L2);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Index info\n",
    "\n",
    "Now we had build"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FieldName:book_intro, IndexName:_default_idx_103, IndexId:443732094176145003\r\n"
     ]
    }
   ],
   "source": [
    "// Check index status\n",
    "IList<MilvusIndexInfo> indexInfos = await collection.DescribeIndexAsync(\"book_intro\");\n",
    "\n",
    "foreach(var info in indexInfos){\n",
    "    Console.WriteLine(\"FieldName:{0}, IndexName:{1}, IndexId:{2}\", info.FieldName , info.IndexName,info.IndexId);\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
